package day_2023_7_to_10;

import java.util.PriorityQueue;

/**
 * @author haomin
 * @date 2023/08/21 23:42
 **/
public class Day_8_21 {
    class Solution {
        public int[] getLeastNumbers(int[] arr, int k) {
            if(arr.length == 0 || k == 0) return new int[0];
            PriorityQueue<Integer> p = new PriorityQueue<>((a, b) -> (b-a));
            for(int i = 0; i < arr.length; ++i) {
                if(i < k) p.offer(arr[i]);
                else {
                    if(p.peek() > arr[i]) {
                        p.poll();
                        p.offer(arr[i]);
                    }
                }
            }
            return p.stream().mapToInt(Integer::intValue).toArray();
        }
    }
}